package org.leetcode.middle.leetcode17;

import java.util.ArrayList;
import java.util.List;

public class Solution
{
    List<String> path = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    //初始对应所有的数字，为了直接对应2-9，新增了两个无效的字符串
    String []numString={"empty","empty","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    public List<String> letterCombinations(String digits) {

        if (digits.length()==0){
            return path;
        }

        backtracking(digits,0);

        return path;
    }

    private void backtracking2(String digits,int digitsIndex){

        if (sb.length()==digits.length()){
            path.add(new String(sb));
            return;
        }

        char numStr = digits.charAt(digitsIndex);
        String str = numString[numStr-'0'];

        for (int i = 0; i < str.length(); i++) {
            sb.append(str.charAt(i));
            backtracking2(digits,digitsIndex+1);
            sb.deleteCharAt(sb.length()-1);
        }
    }

    private void backtracking(String digits,int num)
    {
        if (sb.length()==digits.length()) {
            path.add(sb.toString());
            return;
        }

        String str = numString[digits.charAt(num)-'0'];

        for (int i = 0; i <str.length(); i++) {
            sb.append(str.charAt(i));

            backtracking(digits,num+1);

            sb.deleteCharAt(sb.length()-1);
        }

    }

    public static void main(String[] args) {

//        List<String> path = new ArrayList<>();
//        StringBuilder Sb = new StringBuilder();
//
//        Sb.append("a");
//        Sb.append("b");
//
//        System.out.println(Sb.length());
//
//        path.add(Sb.toString());
//
//        System.out.println(path.get(0));

        String []numString={"empty","empty","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        String digits = "23";

//        System.out.println(digits.charAt(0));

        System.out.println(numString[digits.charAt(0)-'0']);
    }


}
